package com.cellulam.framework.core.utils;

public final class FormatUtils {

    /**
     * 计算百分比，默认保留一位小数
     *
     * @param dividend
     * @param divisor
     * @return
     */
    public static final String calPercentage(Integer dividend, Integer divisor) {
        if (divisor == null || divisor == 0) {
            return "0.0%";
        }

        if (dividend == null || dividend == 0) {
            return "0.0%";
        }
        return toPercentage(dividend * 100 / (double)divisor);
    }

    /**
     * 计算百分比，默认保留一位小数
     *
     * @param dividend
     * @param divisor
     * @return
     */
    public static final String calPercentage(Double dividend, Double divisor) {
        if (divisor == null || divisor == 0) {
            return "0.0%";
        }

        if (dividend == null || dividend == 0) {
            return "0.0%";
        }
        return toPercentage(dividend * 100 / divisor);
    }

    /**
     * 保留一位小数
     *
     * @param value
     * @return
     */
    public static final String toPercentage(double value) {
        return NumberUtil.format(value, 1) + "%";
    }
}
